/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package edu.pdx.cs.guavadata.query;

import edu.pdx.cs.guavadata.Channel;
import edu.pdx.cs.guavadata.Transform;
import edu.pdx.cs.guavadata.data.Common;
import edu.pdx.cs.guavadata.domain.Domain;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 *
 * @author VuTran
 */
public class RowConstant extends Operator {

    List<Object> fields;

    public List<Object> getFields() {
        return fields;
    }

    public void setFields(List<Object> fields) {
        this.fields = fields;
    }

    public RowConstant() {
        // Populate the external view and the fields list
        this.exposedColumns = new ArrayList<String>();
        this.exposedDomains = new ArrayList<Domain>();
        this.fields = new ArrayList<Object>();
    }

    public RowConstant(Collection<String> columnAliases, Collection<Domain> domains,
            Collection<Object> fields) {
        // Populate the external view and the fields list
        this.exposedColumns = new ArrayList<String>(columnAliases);
        this.exposedDomains = new ArrayList<Domain>(domains);
        this.fields = new ArrayList<Object>(fields);

        try {
            if (this.exposedColumns.size() != this.exposedDomains.size())
                throw new Exception("Number of columns does not match number of domains.");
            if (this.exposedColumns.size() != this.fields.size())
                throw new Exception("Number of columns does not match number of values.");
        } catch (Exception e) {
            System.err.println("ERROR: " + e.toString());
        }
    }

    public void addValue(String columnAlias, Domain domain, Object field) {
        this.exposedColumns.add(columnAlias);
        this.exposedDomains.add(domain);
        this.fields.add(field);
    }

    @Override
    public SqlText visit(Provider qv) {
        return qv.transform(this);
    }

    @Override
    public void visit(Channel c, String table) {
        c.mapAliases(this, table);
    }

    @Override
    public void visit(Transform t, Command c) {
        t.expand(this, c);
    }

    @Override
    public void notifyChange(Operator old, Operator newOp) {
        throw new UnsupportedOperationException("The method or operation is not implemented.");
    }

    @Override
    public Operator clone() {
        return new RowConstant(exposedColumns, exposedDomains, fields);
    }

    public String getAsColumnText() {
        String temp = "";
        for (int i = 0; i < exposedColumns.size(); i++) {
            temp = temp + Common.wrap(fields.get(i)) + " AS " + exposedColumns.get(i) + ", ";
        }
        return temp.substring(0, temp.length() - 2);
    }
}
